package thread;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * Author:  LiuTao
 * Version: 1.0
 * Date:    2023-12-12  17:10
 */
class MyThreadPoolExecutor {
    public List<Thread> threadList = new ArrayList<Thread>();
    private BlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(1000);
    public MyThreadPoolExecutor(int n) {
        for(int i = 0; i < n; i++) {
            Thread thread = new Thread(() -> {
                while(true) {
                    try {
                        Runnable runnable = queue.take();
                        runnable.run();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            });
            thread.start();
            threadList.add(thread);
        }
    }

    public void submit(Runnable runnable) throws InterruptedException {
        queue.put(runnable);
    }
}
public class ThreadDemo29 {
    public static void main(String[] args) throws InterruptedException {
        MyThreadPoolExecutor executor = new MyThreadPoolExecutor(4);
        for(int i = 0; i < 1000; i++) {
            int n = i;
                executor.submit(new Runnable() {
                    @Override
                    public void run() {
                        System.out.println("执行任务:" + n + " 当前线程 " + Thread.currentThread().getName());
                    }
            });
        }
    }
}
